package com.gy.hadoop.mr.order;

/*
a.txt
atguigu pingping
atguigu ss
atguigu ss

b.txt
atguigu pingping
atguigu pingping
pingping ss

c.txt
atguigu ss
atguigu pingping


 */

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * 倒排索引案例（多job串联）
 */
public class OneIndexDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        // 输入输出路径需要根据自己电脑上实际的输入输出路径设置
        args = new String[]{"mr/index", "mr/out/index/1"};

        Configuration conf = new Configuration();

        Job job = Job.getInstance(conf);
        job.setJarByClass(OneIndexDriver.class);

        job.setMapperClass(OneIndexMapper.class);
        job.setReducerClass(OneIndexReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        FileSystem fs = FileSystem.get(conf);
        if (fs.exists(new Path(args[1]))) fs.delete(new Path(args[1]),true);

        job.waitForCompletion(true);
    }


    static class OneIndexMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

        String name;
        Text k = new Text();
        IntWritable v = new IntWritable();

        @Override
        protected void setup(Context context) throws IOException, InterruptedException {
            // 获取文件名称
            FileSplit split = (FileSplit) context.getInputSplit();

            name = split.getPath().getName();
        }

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            // 1 获取1行
            String line = value.toString();
            // 2 切割
            String[] fields = line.split(" ");
            for (String word : fields) {
                // 3 拼接
                k.set(word + "--" + name);
                v.set(1);
                // 4 写出
                context.write(k, v);
            }
        }
    }

    static class OneIndexReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
        IntWritable v = new IntWritable();

        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int sum = 0;
            // 1 累加求和
            for (IntWritable value : values) {
                sum += value.get();
            }
            v.set(sum);
            // 2 写出
            context.write(key, v);
        }
    }
}
